package com.gxsyh.nantie.dining.provider.controller;

import com.gxsyh.nantie.commons.configure.rabbitmq.RabbitMQConfig;
import com.gxsyh.nantie.commons.dto.ResponseResult;
import com.gxsyh.nantie.commons.dto.SendLogResult;
import com.gxsyh.nantie.commons.utils.OkHttpClientUtil;
import com.gxsyh.nantie.dining.provider.controller.dto.SaveTbDepartmentSubsidyUpdateTimeParam;
import com.gxsyh.nantie.dining.provider.domain.TbDepartmentSubsidyUpdateTime;
import com.gxsyh.nantie.dining.provider.domain.TbSubsidy;
import com.gxsyh.nantie.dining.provider.service.TbDepartmentSubsidyService;
import com.gxsyh.nantie.dining.provider.service.TbDepartmentSubsidyUpdateTimeService;
import com.gxsyh.nantie.dining.provider.service.TbSubsidyService;
import okhttp3.ResponseBody;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @email yom535@outlook.com
 * @author: 刘锋嘉(Liu Fengjia)
 * @date: 2020/1/8
 * @time: 14:56
 * @Description:
 */
@Transactional
@RestController
public class DiningController {
    @Resource
    TbSubsidyService tbSubsidyService;

    @Resource
    TbDepartmentSubsidyService tbDepartmentSubsidyService;

    @Value("${base.config.leaf.url}")
    String LEAF_URL;

    @Resource
    AmqpTemplate amqpTemplate;

    @Resource
    TbDepartmentSubsidyUpdateTimeService tbDepartmentSubsidyUpdateTimeService;

    /**
     * 查询部门餐别
     * @param departmentId
     * @return
     */
    @GetMapping(value = "/subsidy/select/department")
    public ResponseResult<List<TbSubsidy>> selectDepartmentSubsidy(@RequestParam long departmentId){
        List<TbSubsidy> tbSubsidies = tbSubsidyService.selectDepartmentSubsidy(departmentId);
        return new ResponseResult<>(HttpStatus.OK.value(),"查询部门餐别成功",tbSubsidies);
    }

    /**
     * 保存部门餐别
     * @param subsidy
     * @param departmentId
     * @return
     */
    @PostMapping(value = "/subsidy/save/department/{departmentId}")
    public ResponseResult<Void> saveSubsidyDepartment(@RequestBody TbSubsidy[] subsidy,@PathVariable Long departmentId){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        int i=tbDepartmentSubsidyService.deleteByDepartmentId(departmentId);
        for (TbSubsidy tbSubsidy : subsidy) {
            if (tbSubsidy.getId()==null){
                tbSubsidy.setId(getId(LEAF_URL,"DepartmentSubsidy"));
            }
            Long departmentSubsidyid = getId(LEAF_URL,"DepartmentSubsidy");
            tbDepartmentSubsidyService.insterByDepartmentId(departmentSubsidyid,departmentId,tbSubsidy.getId());
            tbSubsidyService.insterSubsidyBySubsidyId(tbSubsidy.getId(),tbSubsidy.getName(),tbSubsidy.getSubsidyMoney(),tbSubsidy.getNumberSubsidies(),tbSubsidy.getStartime(),tbSubsidy.getEndtime());
        }

        //                使用消息队列发送日志
        SendLogResult sendLogResult = new SendLogResult();
        sendLogResult.setUserId(Long.valueOf(authentication.getName()));
        sendLogResult.setUsername(String.valueOf(departmentId));
        sendLogResult.setInfo("向部门ID为:"+departmentId+"保存了餐别信息:"+ Arrays.toString(subsidy));
        amqpTemplate.convertAndSend(RabbitMQConfig.SEND_LOG_QUEUE,sendLogResult);
//                end消息队列发送日志

        return new ResponseResult<>(HttpStatus.OK.value(),"保存成功",null);
    }

    /**
     * 保存补贴更新时间
     */
    @PostMapping(value = "/subsidy/save/department/time")
    public ResponseResult<Void> saveTbDepartmentSubsidyUpdateTime(@RequestBody SaveTbDepartmentSubsidyUpdateTimeParam saveTbDepartmentSubsidyUpdateTimeParam){
        tbDepartmentSubsidyUpdateTimeService.deleteTbDepartmentSubsidyUpdateTime(saveTbDepartmentSubsidyUpdateTimeParam.getDepartmentsId());
        int resultX =tbDepartmentSubsidyUpdateTimeService.insterTbDepartmentSubsidyUpdateTime(getId(LEAF_URL,"saveSubsidyTime"),saveTbDepartmentSubsidyUpdateTimeParam.getDepartmentsId(),saveTbDepartmentSubsidyUpdateTimeParam.getUpdateTime());
        if (resultX>0){
            return new ResponseResult<>(HttpStatus.OK.value(),"保存成功",null);
        }
        return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"保存失败请检查输入内容",null);
    }

    /**
     * 查询补贴更新时间
     */
    @GetMapping(value = "/subsidy/select/department/time")
    public ResponseResult<TbDepartmentSubsidyUpdateTime> selectTbDepartmentSubsidyUpdateTime(@RequestParam long departmentId){
        TbDepartmentSubsidyUpdateTime tbDepartmentSubsidyUpdateTime = tbDepartmentSubsidyUpdateTimeService.selectOneTbDepartmentSubsidyUpdateTime(departmentId);
        if (tbDepartmentSubsidyUpdateTime!=null){
            tbDepartmentSubsidyUpdateTime.setDepartmentId(null);
            tbDepartmentSubsidyUpdateTime.setId(null);
            return new ResponseResult<>(HttpStatus.OK.value(),"查询补贴更新时间",tbDepartmentSubsidyUpdateTime);
        }else {
            TbDepartmentSubsidyUpdateTime nullTbDepartmentSubsidyUpdateTime=new TbDepartmentSubsidyUpdateTime();
            nullTbDepartmentSubsidyUpdateTime.setDepartmentId(null);
            nullTbDepartmentSubsidyUpdateTime.setId(null);
            nullTbDepartmentSubsidyUpdateTime.setSubsidyUpdateTime("00:00");
            return new ResponseResult<>(ResponseResult.CodeStatus.FAIL,"无补贴更新时间",nullTbDepartmentSubsidyUpdateTime);
        }

    }


    /**
     * 美团雪花算法 获取ID
     * @param LEAF_URL
     * @param Object
     * @return
     */
    public static Long getId(String LEAF_URL,String Object){
        ResponseBody body = OkHttpClientUtil.getInstance().getData(LEAF_URL + Object).body();
        try {
            return Long.valueOf(body.string());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
